﻿using Qy.Core;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Qy.Scenario;

[AppService]
public class OndutyDataService : QyRepository<OndutyData>
{
    private readonly string cacheKey;
    private readonly ApplicationContext _applicationContext;
    public OndutyDataService(ApplicationContext applicationContext) : base(applicationContext)
    {
        cacheKey = GetEntityFullName();
        _applicationContext = applicationContext;
    }

    public async Task<PagedInfo<ViewOndutyData>> GetPageListAsync(QueryOndutyData parm)
    {
        DateTime startDate = DateUtility.GetBeginTime(parm.StartTime, -1);
        DateTime endDate = DateUtility.GetBeginTime(parm.EndTime, 1);
        var exp = Expressionable.Create<OndutyData>();
        exp = exp.AndIF(parm.Cid > 0, data => data.Cid == parm.Cid);
        exp = exp.AndIF(parm.StartTime != null && parm.EndTime != null, data => data.StartTime >= startDate && data.EndTime <= endDate);

        return await Queryable()
            .LeftJoin<OndutyCat>((data, cat) => data.Cid == cat.Id)
            .Where(exp.ToExpression())
            .OrderBy((data) => data.Id, OrderByType.Desc)
            .Select((data, cat) => new ViewOndutyData()
            {
                CatName = cat.Name,
                Id = data.Id.SelectAll()
            })
            .ToPageAsync(parm);
    }

    public async Task<IEnumerable<ViewOndutyData>> GetListAsync(QueryOndutyData parm)
    {
        IEnumerable<ViewOndutyData> ondutys = await _applicationContext.ICacheManager.GetAsync<IEnumerable<ViewOndutyData>>(cacheKey + parm.Cid);
        if (ondutys == null)
        {
            DateTime startDate = DateUtility.GetBeginTime(DateTime.Now, -1);
            var exp = Expressionable.Create<OndutyData>();
            exp = exp.AndIF(parm.Cid > 0, data => data.Cid == parm.Cid);
            exp = exp.And(data => data.StartTime >= startDate);
            exp = exp.And(data => data.Status == true);

            ondutys = await Queryable()
                 .LeftJoin<OndutyCat>((data, cat) => data.Cid == cat.Id)
                 .Where(exp.ToExpression())
                 .OrderBy((data) => data.Id, OrderByType.Desc)
                 .Select((data, cat) => new ViewOndutyData()
                 {
                     CatName = cat.Name,
                     Id = data.Id.SelectAll()
                 })
                 .ToListAsync();
            await _applicationContext.ICacheManager.SetAsync(cacheKey + parm.Cid, ondutys, TimeSpan.FromDays(2));
        }
        return parm.Type switch
        {
            (int)OndutyTypeEnum.Type.大于等于参考时间 => ondutys.Where(o => o.StartTime.CompareTo(Convert.ToDateTime(parm.ReferToTime)) >= 0).Take(parm.PageSize),
            _ => ondutys.Where(o => o.StartTime.CompareTo(Convert.ToDateTime(parm.ReferToTime)) == 0).Take(parm.PageSize),
        };
    }

    public async Task<OndutyData> InsertDelCache(OndutyData ondutyData)
    {
        await _applicationContext.ICacheManager.RemoveAsync(cacheKey + ondutyData.Cid);
        return await InsertAndDelCacheAsync(ondutyData);
    }

    public async Task<int> UpdateDelCache(OndutyData ondutyData)
    {
        OndutyData data = await GetIdAsync(ondutyData.Id);
        if (data == null)
            return 0;
        data.Cid = ondutyData.Cid;
        data.OneLevel = ondutyData.OneLevel;
        data.TwoLevel = ondutyData.TwoLevel;
        data.ThreeLevel = ondutyData.ThreeLevel;
        data.OtherBody = ondutyData.OtherBody;
        data.Phone = ondutyData.Phone;
        data.StartTime = ondutyData.StartTime;
        data.EndTime = ondutyData.EndTime;
        data.Remark = ondutyData.Remark;
        data.Status = ondutyData.Status;
        await _applicationContext.ICacheManager.RemoveAsync(cacheKey + ondutyData.Cid);
        return await UpdateAndDelCacheAsync(data);
    }

    public async Task<int> DeleteDelCache(int id)
    {
        OndutyData data = await GetIdAsync(id);
        if (data == null) 
            return 0;
        int reId = await DeleteAndDelCacheAsync(data.Id);
        if (reId > 0) {
            await _applicationContext.ICacheManager.RemoveAsync(cacheKey + data.Cid);
            return reId;
        }        
        return reId;
    }
}
